using System;
using System.IO;
using System.Text;

namespace ParasiTracer
{
    internal sealed class Logger
    {
        const string LOGS_FILENAME      = "parasilogs.txt";
        const string LOGS_ERR_FORMAT    = "Error: {0}";
        const string LOGS_EXX_FORMAT    = "Xrror: {0}";
        const string LOGS_INFO_FORMAT   = "Info : {0}";
        const string LOGS_MAIN_FORMAT   = "{0:MM/dd/yy H:mm:ss zzz} {1}";

        class Nested
        {
            internal static readonly Logger instance = new Logger();

            static Nested ()
            {
                //
            }
        }

        static Logger()
        { 
            //
        }

        StreamWriter _sw = null;

        Logger ()
        {
            string logFileName = Path.Combine(
                SafeDirectory.inst.getCurrent(),
                LOGS_FILENAME
            );

            _sw = File.AppendText(logFileName);
        }

        public static Logger inst
        {
            get
            {
                return Nested.instance;
            }
        }

        public void logException (Exception exx)
        {
            logFreeString(
                string.Format(LOGS_EXX_FORMAT, exx.ToString())
            );
        }

        public void logInfo(string info)
        {
            logFreeString(
                string.Format(LOGS_INFO_FORMAT, info)
            );
        }

        public void logInfo (string format, params object[] args)
        {
            logInfo(string.Format(format, args));
        }

        public void logError(string error)
        {
            logFreeString(
                string.Format(LOGS_ERR_FORMAT, error)
            );
        }

        public void logError(string format, params object[] args)
        {
            logError(string.Format(format, args));
        }

        public void logFreeString (string @string)
        {
            string msg = string.Format (
                LOGS_MAIN_FORMAT,
                DateTime.Now,
                @string
            );

            _sw.WriteLine   (msg);
            _sw.Flush       ();
        }
    }
}
